package com.hymjweb.demo.hymj.out_unit_manage.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import com.hymjweb.demo.hymj.out_unit_manage.bean.BOutUnitPojo;
import com.hymjweb.demo.hymj.out_unit_manage.mapper.BOutUnitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service
public class BOutUnitService extends BaseService {

    @Autowired
    private BOutUnitMapper bOutUnitMapper;

    @Autowired
    private GlobalCacheService globalCacheService;

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = bOutUnitMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="外发单位管理-新增" , operMemo = "", classType = BOutUnitPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(BOutUnitPojo pojo) throws Exception{
        if(pojo == null){
            throw new Exception("参数错误");
        }
        pojo.setId(getSysStrId());
        checkPojo(pojo);
        //检查UK
        BOutUnitPojo dbBOutUnitPojo = bOutUnitMapper.findUK(pojo.getName(),null);
        if(dbBOutUnitPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }
        int flag = bOutUnitMapper.insert(pojo);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadUnitCache();
        }
        return flag;
    }

    //    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="外发单位管理-修改" , operMemo = "", tableName = "xt_airport")
    @Transactional
    public int update(BOutUnitPojo pojo) throws Exception{
        if(pojo == null){
            throw new Exception("参数错误");
        }
        checkPojo(pojo);
        //检查UK
        BOutUnitPojo dbBOutUnitPojo = bOutUnitMapper.findUK(pojo.getName(),pojo.getId());
        if(dbBOutUnitPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }
        int flag = bOutUnitMapper.update(pojo);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadUnitCache();
        }
        return flag;
    }

    /**
     * 检查必输项
     * @param pojo
     */
    private void checkPojo(BOutUnitPojo pojo) throws Exception{
        DataTypeUtil.isEmptyStrThenThrow(pojo.getId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getName(), SysConstant.XT_DIC_ERROR_MSG.no_name.getName());
    }

    public List<BOutUnitPojo> findCacheAll(){
        List<BOutUnitPojo> pojos = bOutUnitMapper.findCacheAll();
        return pojos;
    }

    //    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="外发单位管理-删除" , operMemo = "", tableName = "xt_airlines")
    @Transactional
    public int delete(String id) throws Exception{
        if(DataTypeUtil.isEmptyStr(id)){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        }
        int flag = bOutUnitMapper.delete(id);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadUnitCache();
        }
        return flag;
    }

}
